<template>
  <div class="table-data">
    <el-form
      :rules="rules"
      ref="ruleForm"
      :model="form"
      size="small"
      class="form-box"
    >

      <!-- <p class="title">基础信息设置</p> -->
      <el-tabs type="border-card">
        <el-tab-pane label="基础信息">
          <el-form-item label="商品标题" prop="title">
            <el-input type="text" v-model="form.title" placeholder="请输入商品标题" ></el-input>
          </el-form-item>
          <!-- <el-form-item label="商品副标题" prop="sub_title">
            <el-input type="text" v-model="form.sub_title" placeholder="请输入商品副标题"></el-input>
          </el-form-item> -->
          <el-form-item prop="cover">
            <label for="verify_hour" class="el-form-item__label form-label">封面图片</label>
            <uploadImg @bindSendImg="propImg" :imgPath="form.cover"></uploadImg>
          </el-form-item>
          <el-form-item label="商品轮播图">
            <uploadImgList @bindSendImgList="propImgList" :imgUrl="form.images"></uploadImgList>
          </el-form-item>
          <el-form-item prop="poster" label="商品海报图">
            <img :src="form.share.share_bg" @click="codeediting" v-if="form.share.share_bg" class="share_bg">
            <posterUploadImg @bindSendImg="posterImg" :imgUrl="form.share.share_bg" v-if="!form.share.share_bg"></posterUploadImg>
          </el-form-item>
          <el-form-item label="商品类型" prop="verify_type">
            <el-radio-group v-model="form.verify_type ">
              <!-- <el-radio :label="0">物流商品</el-radio>
              <el-radio :label="1">核销商品</el-radio> -->
              <el-radio :label="'ship'">物流商品</el-radio>
              <el-radio :label="'code'">核销商品</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="制码模式" prop="ticket_type">
            <el-radio-group v-model="form.ticket_type" >
              <el-radio :label="1">自建商品</el-radio>
              <el-radio :label="2" :disabled="form.verify_type!=='ship'?false:true">导码商品</el-radio>
              <el-radio :label="3" :disabled="form.verify_type!=='ship'?false:true">制码商品</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="售卖时间">
            <el-date-picker
                    v-model="saleDateTime"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    end-placeholder="结束日期"
                    :default-time="['00:00:00','23:59:59']"
                    >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="是否可退" prop="refund_able">
            <el-radio-group v-model="form.refund_able">
              <el-radio :label="1">可以退</el-radio>
              <el-radio :label="0">不可退</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="上下架状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio :label="1">上架</el-radio>
              <el-radio :label="2">下架</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="供应商" prop="vendor_id">
            <el-select v-model="form.vendor_id" filterable clearable placeholder="供应商">
              <el-option v-for="item in supplierList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属商家" prop="shop_id"   v-if="form.vendor_id==1">
            <el-select v-model="form.shop_id" filterable clearable placeholder="所属商家">
              <el-option v-for="item in businessList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品所属标签" prop="tag_ids">
            <el-select v-model="form.tag_ids" multiple placeholder="请选择">
              <el-option
                      v-for="item in labelList"
                      :key="item.id"
                      :label="item.tag"
                      :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品所属分类">
            <!-- <el-cascader
              :options="form.location"
              v-model="kindList"
              @change="handleChange">
            </el-cascader> -->
            <el-select v-model="form.cate_id" clearable placeholder="一级分类" @change="handleChange">
              <el-option v-for="item in kindList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
            <el-select v-model="form.sub_cate_id" clearable placeholder="二级分类" @change="handleChangeTwo" style="margin:0 10px">
              <el-option v-for="item in kindListTwo" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
            <el-select v-model="form.third_sub_cate_id" clearable placeholder="三级分类">
              <el-option v-for="item in kindListThree" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-tab-pane>
        <!-- <el-tab-pane label="购买须知">
          <el-form-item label="" prop="application">
            <ueditor :content="form.content" :key="timer" @ueditor-change="editorChange"></ueditor>
          </el-form-item>
        </el-tab-pane> -->
        <el-tab-pane label="商品详情">
          <ueditor :content="form.describe" :key="timer" @ueditor-change="editorChange"></ueditor>
        </el-tab-pane>
        <el-tab-pane label="核销码">
          <el-form-item label="与商家结算时机" prop="verify_way">
            <el-select v-model="form.verify_way" clearable placeholder="请选择">
              <el-option label="核销后几天" value="1" v-if="form.verify_type!=='ship'"></el-option>
              <el-option label="发货后几天" value="2" v-if="form.verify_type!=='code'"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="具体几天" prop="verify_day">
            <el-input type="number" min="0" v-model="form.verify_day"></el-input>
          </el-form-item>
          <el-form-item v-if="form.verify_type!='ship'" label="一个商品生成的核销数码数量（无关规格）" prop="create_ticket_num">
            <el-input type="number" min="1" v-model="form.create_ticket_num"></el-input>
          </el-form-item>
          <el-form-item v-if="form.verify_type!='ship'" label="核销码保护期">
            <el-input type="text" v-model="form.verify_hour"></el-input>
          </el-form-item>
          <!-- <el-form-item v-if="form.verify_type!='ship'" label="核销码有效期" prop="dateTime"> -->
          <el-form-item v-if="form.verify_type!='ship'">
            <label for="verify_hour" class="el-form-item__label form-label">核销码有效期</label>
            <el-date-picker
              v-model="dateTime"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00','23:59:59']"
              >
            </el-date-picker>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="价格设置">
          <el-row>
            <el-col :span="24">
              <el-form-item label="友情提醒"><label for="">商家结算价 <= 渠道结算价 <= 建议零售价 <= 原价</label></el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="商品规格" prop="guige">
                <el-radio-group v-model="guige">
                  <el-radio :label="1">单规格</el-radio>
                  <el-radio :label="2">多规格</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col v-if="guige==2">
              <el-form-item label="商品规格" prop="spec">
                <div class="specBox">
                  <ul>
                    <li v-for="(specItem, i) in specItems" :key="i">
                      <el-input v-model="specItem.standardValue" style="width: 220px"  @blur.prevent="setParentInput(specItem.standardValue, i)" placeholder="请填写规格名称(如：颜色)"></el-input>
                      <el-input v-model="specItem.specName" style="width: 220px" placeholder="请填写规格值(如：红色)"></el-input>
                      <el-button type="primary" class="button" size="small" @click="addSpecVal(i)">添加</el-button>
                      <el-button type="danger" size="small" @click="delliEle(i)">删除</el-button>
                      <div>
                        <el-tag v-for="(item,index) in specItem.specNames" closable :disable-transitions="false" :key="item" @close="closeTag(i, index)">{{item}}</el-tag>
                      </div>
                    </li>
                  </ul>
                  <el-button type="primary" size="small" @click="add()">添加规格</el-button>
                  <el-form>
                    <table class="specTable" v-if="show">
                      <thead>
                      <th v-for="(th,i) in arr" :key="i">{{th}}</th>
                      <th v-if="form.verify_type!='ship'">每个核销码商家结算价</th>
                      <th>商家结算价</th>
                      <th v-if="form.verify_type!='ship'">每个核销码默认渠道结算价</th>
                      <th>渠道商结算价</th>
                      <th>原价</th>
                      <th>建议售价</th>
                      <th>库存</th>
                      <th>重量（单位kg）</th>
                      <!-- <th>操作</th> -->
                      </thead>
                      <tbody>
                      <tr v-for="(tr, index) in result_spec" :key="index">
                        <td v-for="(td,i) in tr.td" :key="i">{{td}}</td>
                        <td><el-input size="mini" @blur="getSellPrice(result_spec[index].sell_price, index)" v-model.number="result_spec[index].sell_price" type="number" min="1"></el-input></td>
                        <td v-if="form.verify_type!='ship'">{{result_spec[index].supplierPrice}}</td>
                        <td><el-input size="mini"  @blur="getChannelPrice(result_spec[index].channel_settle_price, index)" type="number" min="1" v-model.number="result_spec[index].channel_settle_price"></el-input></td>
                        <td v-if="form.verify_type!='ship'">{{result_spec[index].channelPrice}}</td>
                        <td><el-input size="mini" @blur="getOriginalPrice(result_spec[index].original_price, index)" type="number" min="1" v-model.number="result_spec[index].original_price"></el-input></td>
                        <td><el-input size="mini" @blur="getSuggestPrice(result_spec[index].suggest_price, index)" type="number" min="1" v-model.number="result_spec[index].suggest_price"></el-input></td>
                        <td><el-input size="mini" @blur="getStock(result_spec[index].stock, index)" type="number" min="1" v-model.number="result_spec[index].stock"></el-input></td>
                        <td><el-input size="mini" type="number" min="1" v-model="result_spec[index].weight"></el-input></td>
                      </tr>
                      </tbody>
                    </table>
                  </el-form>
                </div>
              </el-form-item>
            </el-col>
            <el-col v-else>
              <el-col :span="24" v-if="form.verify_type=='ship'">
                <p class="price-title">价格设置自营物流产品</p>
                <el-form-item label="商家结算价" prop="merchant_settle_price">
                  <el-input type="number" @blur="getSellPrices(form.merchant_settle_price)" min="1" v-model.number="form.merchant_settle_price"></el-input>
                </el-form-item>
                <el-form-item label="渠道结算价" prop="channel_settle_price">
                  <el-input type="number" @blur="getChannelPrices(form.channel_settle_price)" min="1" v-model.number="form.channel_settle_price"></el-input>
                </el-form-item>
                <el-form-item label="原价" prop="original_price">
                  <el-input type="number" @blur="getOriginalPrices(form.original_price)" min="1" v-model.number="form.original_price"></el-input>
                </el-form-item>
                <el-form-item label="建议零售价" prop="suggest_price">
                  <el-input type="number"  @blur="getSuggestPrices(form.suggest_price)" min="1" v-model.number="form.suggest_price"></el-input>
                </el-form-item>
                <el-form-item label="库存" prop="stock">
                  <el-input type="number" min="1" v-model="form.stock"></el-input>
                </el-form-item>
                <el-form-item label="重量（单位kg）" prop="weight">
                  <el-input type="number" min="0" v-model="form.weight"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24" v-else>
                <el-form-item label="每个核销码供应商结算价" prop="vendor_settle_price">
                  <el-input type="number"  min="1" v-model.number="form.vendor_settle_price" @blur="getSupplierPrices(form.vendor_settle_price)"></el-input>
                </el-form-item>
                <el-form-item label="单商品供应商算价">
                  <span class="price-txt">{{supplierPrice}}</span>
                  <span class="price-tip">每个核销码供应商结算价*单个商品生成码数量</span>
                </el-form-item>
                <el-form-item label="每个核销码默认渠道结算价" prop="channel_settle_price">
                  <el-input type="number"  min="1" v-model.number="form.channel_settle_price" @blur="getchannelPricess(form.channel_settle_price)"></el-input>
                </el-form-item>
                <el-form-item label="单商品渠道结算价">
                  <span class="price-txt">{{channelPrice}}</span>
                  <span class="price-tip">每个核销码默认渠道结算价*单个商品生成码数量</span>
                </el-form-item>
                <el-form-item label="原价" prop="original_price">
                  <el-input type="number" @blur="getOriginalPrices(form.original_price)" min="1" v-model.number="form.original_price"></el-input>
                </el-form-item>
                <el-form-item label="建议售价（单个商品，非单个核销码）" prop="suggest_price">
                  <el-input type="number" @blur="getSuggestPrices(form.suggest_price)" min="1" v-model.number="form.suggest_price"></el-input>
                </el-form-item>
                <el-form-item label="库存" prop="stock">
                  <el-input type="number" min="1" v-model="form.stock"></el-input>
                </el-form-item>
                <!-- <el-form-item label="重量（单位g）" prop="weight">
                  <el-input type="number" min="1" v-model="form.weight"></el-input>
                </el-form-item> -->
              </el-col>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="物流设置" v-if="form.verify_type=='ship'">
          <el-form-item label="物流信息" >
            <el-radio-group v-model="form.client" @change="shippingChange()">
              <el-row>
                <el-col :span="24" style="margin-bottom:18px">
                  <el-radio :label="0">统一运费</el-radio>
                  <el-input type="text" v-model="form.shipping_price" @focus="shippingChange1(0)"></el-input>
                </el-col>
                <el-col :span="24">
                  <el-radio :label="1">运费模板</el-radio>
                  <el-select v-model="form.shipping_id" clearable placeholder="请选择" @focus="shippingChange1(1)">
                    <el-option v-for="item in shippingTemplateList" :key="item.id" :label="item.name" :value="item.id" ></el-option>
                  </el-select>
                </el-col>
              </el-row>
            </el-radio-group>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="留言模板">
          <el-form-item label="留言模板" prop="mt_id">
            <el-radio-group>
              <el-row>
                <el-col :span="24">
                  <el-select v-model="form.mt_id" clearable placeholder="请选择">
                    <el-option v-for="mtItem in messageTemplateList"  :key="mtItem.mt_id" :label="mtItem.mt_name" :value="mtItem.mt_id"></el-option>
                  </el-select>
                </el-col>
              </el-row>
            </el-radio-group>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="购买须知">
            <ueditor :content="form.remark" :key="timer" @ueditor-change="editorChangeRemark"></ueditor>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="cancel" size="small">取消</el-button>
      <el-button @click="submitFormAdd('ruleForm')" type="primary" size="small">确定</el-button>
    </div>
    <AppletPoster :isImport="isImport" :isPoster="isPoster" :key="timer" :posterImg="form.share.share_bg"  @closeDialog="closeDialog" @closeDialog1="closeDialog1" :formDate="form" @determine="determine"></AppletPoster>
  </div>
</template>

<script lang="ts">
  import { Component, Vue, Prop, Provide, Watch } from "vue-property-decorator";
  import ueditor from "@/components/ueditor/index.vue";
  import uploadImg from "@/components/uploadImg/index.vue";
  import posterUploadImg from "@/components/uploadImg/poster.vue";
  import uploadImgList from "@/components/uploadImg/list.vue";
  import goodsApi from "@/api/goods";
  import businessApi from "@/api/business";
  import supplierApi from "@/api/supplier";
  import AppletPoster from "./AppletPosterAdd.vue";
  @Component({
    components: {AppletPoster,posterUploadImg,uploadImg,uploadImgList,ueditor}
  })
  export default class goodsAddDialog extends Vue {
    // 属性或参数中使用 ！：表示强制解析（告诉typescript编译器，这里一定有值），常用于vue-decorator中的@Prop
    check:boolean = true//防重复提交
    @Provide() timer:any=''
    @Provide() dateTime: any = [];//核销时间
    @Provide() saleDateTime: any = [];//售卖时间
    @Provide() value1: any = [];
    @Provide() imgList: any=[];//轮播列表
    @Provide() labelList: any=[];//标签列表
    @Provide() supplierList: any=[];//供应商列表
    @Provide() businessList: any=[];//商家列表
    @Provide() advertiser: any = [];//广告商
    @Provide() kindList: any = [];//所属分类列表
    @Provide() shippingTemplateList: any = [] //运费模板列表
    @Provide() messageTemplateList: any = [] //留言模板
    @Provide() kindListTwo: any = [];//所属分类列表
    @Provide() kindListThree: any = [];//所属分类列表
    @Provide() state: boolean = false; // 状态默认值
    @Provide() guige: any = 1; // 规格
    @Provide() supplierPrice: any = ""; // 供应商算价
    @Provide() channelPrice: any = ""; // 渠道结算价
    @Provide() isPoster: Boolean = false; // 是否展示导出弹窗
    @Provide() isImport: Boolean = false; // 是否展示导入弹窗
    @Provide() searchMapS:{//供应商查询条件
      name: string,
      phone:string,
    }={
      name:'',
      phone:''
    }
    @Provide() searchMapB:{
      keyword: string,
      cate_id: string,
      is_child: any,
    }={
      keyword: "",
      cate_id: "",
      is_child: 0
    }
    @Provide() searchItem:{
      item: string,
      parent_id: string,
      goods_id: any,
    }={
      item: "",
      parent_id: "",
      goods_id: ""
    }
    @Provide() searchItem2:{
      item: string,
      parent_id: string,
      goods_id: any,
    }={
      item: "",
      parent_id: "",
      goods_id: ""
    }
    @Provide() form:any={
      id:"",
      barcode:"",
      title:'',
      sub_title:'',
      cate_id:'',
      sub_cate_id:'',
      third_sub_cate_id:'',
      brand_id:'',
      stock:'',
      shipping_id:'',
      mt_id:0,
      shipping_price:'',
      weight:'',
      cubage:'',
      status:1,
      price:'',
      client:'',
      cover:'',
      sale_on_time:'',
      sale_off_time:'',
      original_price:'',
      vendor_price:'',
      vendor_settle_price:'',
      channel_settle_price:'',
      merchant_settle_price:'',
      suggest_price:'',
      unit_name:'',
      vendor_id:'',
      shop_id:'',
      refund_able:'',
      ticket_type:'',
      verify_type:'',
      verify_way:'',
      verify_day:'',
      verify_start_time:'',
      verify_end_time:'',
      verify_hour:'',
      verify_ticket_num:'',
      create_ticket_num:'',
      describe:'',
      remark:'',
      images:[],
      spec_item:[],
      spec_goods_price:[],
      share:{
        type:1,
        share_bg:'',
        qr_y:0,
        qr_x:0
      }
    };
    rules:any = {
      title: [{ required: true, message: "请输入商品标题", trigger: "blur" }],
      stock: [{ required: true, message: "请输入库存", trigger: "change" }],
      shipping_price: [{ required: true, message: "请输入运费价格", trigger: "blur" }],
      vendor_id: [{ required: true, message: "请选择供应商", trigger: "change" }],
      shop_id: [{ required: true, message: "请选择所属商家", trigger: "change" }],
      // tag_ids: [{ required: true, message: "请输选择标签", trigger: "blur" }],
      weight: [{ required: true, message: "请输入重量", trigger: "blur" }],
      price: [{ required: true, message: "请输入售价", trigger: "blur" },{
        //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
        pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
        message: '请输入大于0的数'
      }],
      original_price: [{ required: true, message: "请输入原价", trigger: "blur" },{
        //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
        pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
        message: '请输入大于0的数'
      }],
      channel_settle_price: [{ required: true, message: "请输入渠道结算价", trigger: "blur" }
      // ,{
      //   //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
      //   pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
      //   message: '请输入大于0的数'
      // }
      ],
      vendor_settle_price: [{ required: true, message: "请输入供应商结算价", trigger: "blur" }
      // ,{
      //   //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
      //   pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
      //   message: '请输入大于0的数'
      // }
      ],
      merchant_settle_price: [{ required: true, message: "请输入商家结算价", trigger: "blur" },{
        //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
        pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
        message: '请输入大于0的数'
      }],
      suggest_price: [{ required: true, message: "请输入建议零售价", trigger: "blur" },{
        //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
        pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
        message: '请输入大于0的数'
      }],
      refund_able: [{ required: true, message: "请选择是否可退", trigger: "change" }],
      status: [{ required: true, message: "请选择上下架", trigger: "change" }],
      ticket_type: [{ required: true, message: "请选择制码模式", trigger: "change" }],
      verify_type: [{ required: true, message: "请选择商品类型", trigger: "change" }],
      verify_way: [{ required: true, message: "请选择与商家结算时机", trigger: "change" }],
      verify_day: [{ required: true, message: "请输入具体几天", trigger: "blur" },{
        //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
        pattern: /^([1-9]\d*(\.\d+)?|0)$/,  //正则
        message: '请输入大于等于0的数'
      }],
      verify_hour: [{ required: true, message: "请输入核销码保护期", trigger: "blur" },{
        //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
        pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
        message: '请输入大于0的数'
      }],
      describe: [{ required: true, message: "请输入商品详情描述", trigger: "blur" }],
      verify_ticket_num: [{ required: true, message: "请输入单个商品可使用次数", trigger: "blur" },{
        //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
        pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
        message: '请输入大于0的数'
      }],
      create_ticket_num: [{ required: true, message: "请输入单个商品生成核销码数量", trigger: "blur" },{
        //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
        pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
        message: '请输入大于0的数'
      }],
    };
    @Provide() specItems:any= []  // 规格数组
    @Provide() spec:any= []  //
    @Provide() result_spec:any= []  //
    @Provide() show:boolean= false  //
    @Provide() disable:boolean= false  //
    @Provide() arr:any= []  //
    @Provide() addSpec:any= []  //
    @Provide() result:any= []  //
    @Provide() seleIndex:any= []  //多规格选中输入框的下标（商家结算价）
    @Provide() seleVal:any= []  //多规格选中输入框的值（商家结算价）
    @Provide() channelIndex:any= []  //多规格选中输入框的下标（渠道结算价）
    @Provide() channelVal:any= []  //多规格选中输入框的值（渠道结算价）
    // 监听商家结算价
    @Watch("form.vendor_settle_price")
    getSupplierPrice(){
      this.supplierPrice=this.form.vendor_settle_price*this.form.create_ticket_num
    }
    getSupplierPrices(val){
    }
    @Watch("form.channel_settle_price")
    getchannelPrices(){
      this.channelPrice=this.form.channel_settle_price*this.form.create_ticket_num
    }
    getchannelPricess(val){
      if(val<this.form.vendor_settle_price){
        this.$message({
          message: '渠道结算价必须大于供应商结算价',
          type: "error"
        });
      }
    }
    @Watch("form.create_ticket_num")
    getTicketNum(){
      this.supplierPrice=this.form.vendor_settle_price*this.form.create_ticket_num
      this.channelPrice=this.form.channel_settle_price*this.form.create_ticket_num
      // this.trsVals.forEach(item=>{
      //   item.supplierPrice=item.sell_price*this.form.create_ticket_num
      //   item.channelPrice=item.channel_settle_price*this.form.create_ticket_num
      // })
    }
    @Watch("form.verify_type")
      getVerifyType(Val: any, oldVal: any) {
        if(Val=='ship') {
          this.form.ticket_type = 1
        }
      }
    getSellPrices(val){
    }
    getChannelPrices(val){
      if(val<this.form.merchant_settle_price){
        this.$message({
          message: '渠道结算价必须大于商家结算价',
          type: "error"
        });
      }
    }
    getOriginalPrices(val){
      if(val < this.form.suggest_price || val<this.form.channel_settle_price){
        this.$message({
          message: '原价必须大于等于建议零售价、商家结算价、渠道结算价',
          type: "error"
        });
      }
    }
    getSuggestPrices(val){
      if(val>this.form.original_price){
        this.$message({
          message: '建议零售价必须小于等于原价',
          type: "error"
        });
        return;
      }
    }
    // 验证每个核销码商家结算价是否大于0
    getSellPrice(val, index) {
      if (Number(val) <= 0) {
        this.result_spec[index].sell_price = null;
        this.$message({
          message: '商家结算价必须大于0',
          type: "error"
        });
        return;
      }
    }
    // 验证每个核销码渠道结算价是否大于0
    getChannelPrice(val, index) {
      if (Number(val) <= 0) {
        this.result_spec[index].channel_settle_price = null;
        this.$message({
          message: '渠道结算价必须大于0',
          type: "error"
        });
        return;
      }
      if(val<this.result_spec[index].sell_price){
        this.$message({
          message: '渠道结算价必须大于商家结算价',
          type: "error"
        });
      }
    }
    // 验证原价是否大于0
    getOriginalPrice(val, index) {
      if (Number(val) <= 0) {
        this.result_spec[index].original_price = null;
        this.$message({
          message: '原价必须大于0',
          type: "error"
        });
        return;
      }
      if(val<this.result_spec[index].suggest_price || val<this.result_spec[index].channel_settle_price){
        this.$message({
          message: '原价必须大于等于建议零售价、商家结算价、渠道结算价',
          type: "error"
        });
      }
    }
    // 验证建议零售价是否大于0
    getSuggestPrice(val, index) {
      if (Number(val) <= 0) {
        this.result_spec[index].suggest_price = null;
        this.$message({
          message: '建议售价必须大于0',
          type: "error"
        });
        return;
      }
      if(Number(val) > this.result_spec[index].original_price){
        this.$message({
          message: '建议零售价必须小于等于原价',
          type: "error"
        });
        return;
      }
    }
    // 验证库存是否大于0
    getStock(val, index) {
      if (Number(val) <= 0) {
        this.result_spec[index].stock = null;
        this.$message({
          message: '库存必须大于0',
          type: "error"
        });
        return;
      }
    }
    shippingChange1(i:any) {
      this.form.client = i
    }
    //物流设置
    shippingChange(){
      if(this.form.client == 0){
        this.form.shipping_id = ''
      }else{
        this.form.shipping_price =''
      }
    }
  
    // 添加规格项
    add() {
      this.specItems.push({
        standardValue: '',
        specNames: [],
        specName: '',
      });
    }
    // 删除规格值
    closeTag(i, index) {
      this.specItems[i].specNames.splice(index, 1);
      this.form.spec_item[i].children.splice(index, 1);
      // this.addSpec[i].children.splice(index, 1);
      // 规格值数组为空，则删除当前规格
      if (!this.specItems[i].specNames.length) {
        this.delliEle(i);
      }else{
        this.creatTable()
      }
    }
    // 删除规格项
    delliEle(index) {
      this.specItems.splice(index, 1);
      this.addSpec.splice(index, 1);
      if(this.form.spec_item.length>this.specItems.length){
        this.form.spec_item.splice(index, 1);
      }
      if (this.specItems.length === 0) {
        this.show = false;
      }
      this.arr.splice(index, 1);
      this.creatTable();
    }
    setParentInput(val, index) {
      this.arr[index] = val;
      let  newSpecItem = {};
      for (let item in this.specItems) {
        newSpecItem[this.specItems[item].standardValue] = this.specItems[item].specNames;
      }
      if (newSpecItem) {
        this.form.spec_item = newSpecItem;
      }
    }
    addSpecVal(index) {
      for (let i = 0; i < this.specItems.length - 1; i++) {
        // if (this.specItems[index].standardValue === this.specItems[i].standardValue) {
        //   console.log(this.specItems[index])
        //   console.log(this.specItems[i])
        //   this.specItems[index].standardValue = '';
        //   this.specItems[index].specName = '';
        //   return;
        // }
      }
      if (!this.specItems[index].specName || !this.specItems[index].standardValue) return;
      this.$set(this.specItems[index], 'isTrigger', true);
      // 判断重复添加
      for (let i = 0; i < this.specItems[index].specNames.length; i++) {
        if (this.specItems[index].specName === this.specItems[index].specNames[i]) {
          return;
        }
      }
      this.specItems[index].specNames.push(this.specItems[index].specName);
      this.show = true;
      this.disable = true;
      // 请求添加规格接口并组装规格参数
      this.searchItem.item=this.specItems[index].standardValue
      this.searchItem.parent_id='0'
      let a={id:"",item:"",parent_id:"",children:[]}
      let arr:any=[]
      if(this.specItems.length>this.addSpec.length){
        goodsApi.saveItem(this.searchItem)
                .then((res: any) => {
                  a.id=res.data.data.id
                  a.item=res.data.data.item
                  a.parent_id=res.data.data.parent_id
                  this.addSpec.push(a)
                  this.searchItem2.item=this.specItems[index].specName
                  this.searchItem2.parent_id=a.id
                  this.specItems[index].specName = '';
                  goodsApi.saveItem(this.searchItem2)
                          .then((res: any) => {
                            let b={id:"",item:"",parent_id:a.id,children:[]}
                            b.id=res.data.data.id
                            b.item=res.data.data.item
                            this.addSpec[index].children.push(b)
                            this.addSpec.forEach(item=>{
                              if(item.children.length>0){
                                arr.push(item)
                              }
                            })
                            this.form.spec_item=arr
                            this.creatTable();
                          });
                })
      }
      else{
        this.searchItem2.item=this.specItems[index].specName
        this.searchItem2.parent_id=this.addSpec[index].id
        this.specItems[index].specName = '';
        goodsApi.saveItem(this.searchItem2)
                .then((res: any) => {
                  let b={id:"",item:"",parent_id:this.addSpec[index].id,children:[]}
                  b.id=res.data.data.id
                  b.item=res.data.data.item
                  this.addSpec[index].children.push(b)
                  let arr:any=[]
                  this.addSpec.forEach(item=>{
                    if(item.children.length>0){
                      arr.push(item)
                    }
                  })
                  this.form.spec_item=arr
                  this.creatTable();
                });
      }
    }
    creatTable() {
      let arr1:any=[]
      if(this.addSpec.length>0){
        this.addSpec.forEach((items:any,idx:any)=>{
          let arr2:any=[]
          items.children.forEach((item:any,i:any)=>{
            arr2.push({
              key:items.id+":"+item.id,
              key_name : items.item + ":" + item.item,
              td:item.item,
            })
          })
          arr1.push(arr2);
        })
        this.specList(arr1);
      }else{
        this.result_spec=[]
        this.form.spec_goods_price=this.result_spec
      }

    }

    specList(arr){
      let result:any = []
      let arrs=arr.shift()
      this.result_spec = arrs
      arr.forEach(item =>{
        this.specItem(this.result_spec,item)
      })
      this.result_spec.forEach(item=>{
        item.td=item.td.split(',')
      })
      this.form.spec_goods_price=this.result_spec
    }
    // 组合规格
    specItem(arr1,arr2){
      let result:any = []
      arr1.forEach(element => {
        arr2.forEach(element2 => {
          let a=element.key_name + '_' + element2.key_name
          result.push({
            key:element.key + '_' + element2.key,
            key_name:element.key_name + '_' + element2.key_name,
            td:element.td+','+element2.td,
            bar_code:"",//	条形码
            sku:"",//sku
            status:1,//状态
            price:"",//	规格售价
            original_price:0,//	原价
            suggest_price:"",//	建议售价
            channel_settle_price:"",//	渠道商结算价
            sell_price:"",//商家结算价
            platform_price:"",//供应商结算价
            vendor_price:"",//供应价
            stock: 0,//	库存
            spec_img:"",
            sale_stock:"",
            // channelPrice:"",//渠道结算价
            // supplierPrice:"",//商家结算价
          })
        })
      });
      this.result_spec = result;
    }
    // 一级分类
    handleChange(value:any) {
      this.kindList.forEach((item:any)=>{
        if(item.id==value){
          this.kindListTwo=item.children
        }
      })
    }
    // 二级分类
    handleChangeTwo(value){
      this.kindListTwo.forEach((item:any)=>{
        if(item.id==value){
          this.kindListThree=item.children
        }
      })
    }
    // 上传图片子组件传递过来的图片路径
    propImg(imgPath: any){
      this.form.cover = imgPath;
      console.log(imgPath)
      console.log(this.form.cover)
    }
    codeediting() {
      this.isPoster = true
      this.isImport = true
    }
     // 海报
    posterImg(imgPath: any){
      this.form.share.share_bg = imgPath;
      this.form.share.type = 1
      this.form.share.qr_y = 0
      this.form.share.qr_x = 0
      this.isPoster = true
      this.isImport = true
    }
    determine(value) {
      console.log(value)
      this.form.share = value
      this.isPoster = true;
      this.isImport=false
    }
    //删除海报
    closeDialog() {
      this.isPoster = true;
      this.isImport=false
      this.form.share = {
        type:null,
        share_bg:'',
        qr_y:'',
        qr_x:''
      }
    }
    // 关闭弹窗
    closeDialog1() {
      this.isPoster = false;
      this.isImport=false
    }
    // 上传多张图片子组件传递过来的图片路径
    propImgList(imgPath: any){
      this.form.images = imgPath;
    }
    // 富文本编辑
    editorChange(data:any) {
      this.form.describe = data.content;
    }

    // 富文本编辑器--购买须知
    editorChangeRemark(data:any) {
      this.form.remark = data.content;
    }
    // 取消
    cancel(){
      this.$router.push({
        path:'/goodsList'
      })
    }
    // 新建广告商
    handleAdd(){
      let a={id:'' ,code:'',weight:this.form.advertisers.length+1}
      this.form.advertisers.push(a)
    }
    // 提交
    submitFormAdd(formName: any) {
      (this.$refs[formName] as any).validate((valid: boolean) => {
        if(this.form.verify_type=='ship' && this.form.client!==0 && this.form.client!==1) {
          valid = false
        }
        if(this.form.share.type==='' && this.form.share.share_bg==='') {
          this.form.share = {
            type:'',
            share_bg:'',
            qr_y:'',
            qr_x:''
          }
        }
        if (valid) {
          if(this.dateTime==null){
            this.dateTime=["",""]
          }
          if(this.saleDateTime==null){
            this.saleDateTime=["",""]
          }
          this.form.verify_start_time=this.dateTime[0];
          this.form.verify_end_time=this.dateTime[1];
          this.form.sale_on_time=this.saleDateTime[0];
          this.form.sale_off_time=this.saleDateTime[1];
          if(this.guige==1){
            this.form.spec_item=[];
            this.form.spec_goods_price=[];
            if(this.form.verify_type!=='ship') {
              this.form.merchant_settle_price = this.form.vendor_settle_price;
            }
          }
          // this.form.spec_goods_price.forEach(item=>{
          // delete item.td
          // });
          console.log(this.form.share)
          if(this.check) {
            let _this = this
            _this.check = false
            goodsApi.saveGoods(this.form)
            .then((res: any) => {
              if(res.data.code){
                _this.$message({
                  message: '操作成功',
                  type: "success"
                });
                _this.$router.push({
                  path:'/goodsList'
                })
                _this.$emit("loadData")
                setTimeout(function(){
                  _this.check = true
                },3000)
              }else{
                _this.$message({
                  message: res.data.msg,
                  type: "error"
                });
                _this.check = true
              }
            });
          }
          
        } else {
          this.$confirm('商品信息未完善，请核查', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          })
        }
      });
    }
    handleAvatarSuccess(res:any, file:any) {
      this.form.imgPath = res.data.fileUrl;
    }
    created() {
      // 标签
      goodsApi.labelList(1,9999999).then((res:any) => {
        if(res.data.code==200) {
          this.labelList = res.data.data.data;
        }
      });
      // 商家
      businessApi.businessList(1,999999,this.searchMapB).then((res:any) => {
        if(res.data.code==200) {
          this.businessList = res.data.data.list;
        }
      });
      // 供应商
      supplierApi.supplierList(1,999999,this.searchMapS).then((res:any) => {
        if(res.data.code==200) {
          this.supplierList = res.data.data.lists.data;
        }
      });
      // 分类
      goodsApi.goodsKind().then((res:any) => {
        if(res.data.code==200) {
          this.kindList = res.data.data;
        }
      });
      //运费模板
      goodsApi.shippingTemplate().then((res:any) => {
        this.shippingTemplateList = res.data.data
      })

      //留言模板
      goodsApi.messageTemplate().then((res:any) => {
        this.messageTemplateList = res.data.data
      })
    }
  }
</script>

<style lang="scss" scoped>
  .dialog-footer{
    padding-left: 50px
  }
  .spec-box{
    border: 1px solid #d8d8d8;
    min-height: 200px;
    max-height: 800px;
    overflow-y: scroll;
    padding: 10px;
    .el-input,
    .el-select {
      margin-right: 10px;
      margin-bottom: 10px;
    }
  }
  .form-box {
    margin-bottom: 20px;
    .el-input,
    .el-select {
      width: 200px !important;
    }
    .title{
      line-height: 60px;
      font-weight: bold;
      font-size: 16px;
    }
    .price-title{
      font-size: 14px;
      font-weight: bold;
      margin-bottom: 20px;
    }
    .price-txt{
      font-size: 14px;
      color: #333;
    }
    .price-tip{
      color: red;
      font-size: 12px;
      margin-left: 50px;
    }
    .specBox {
      width: 75%;
      min-height: 200px;
      max-height: 800px;
      overflow-y: scroll;
      padding: 10px;
      border: 1px solid #d8d8d8;
      .specTable {
        border: 1px solid #e3e3e3;
        thead {
          background: #f5f5f5;
        }
        th,td {
          width: 120px;
          padding: 3px 8px;
          text-align: center;
          border: 1px solid #e3e3e3;
          .el-input{
            width: 100px !important;
          }
        }
      }
    }
  }
  .share_bg {
    width: 148px;
    height: 148px;
  }
</style>
